<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="86px">
      <el-form-item label="原始文件名" prop="originalFilename">
        <el-input
            v-model="queryParams.originalFilename"
            placeholder="请输入原始文件名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Upload"
            @click="openUploadDialog = true"
            v-hasPermission="['system:file:import']"
        >上传</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList">
      <el-table-column label="文件id" align="center" prop="id" :show-overflow-tooltip="true"/>
      <el-table-column label="文件地址" align="center" prop="url" :show-overflow-tooltip="true"/>
      <el-table-column label="文件大小" align="center" prop="size" />
      <el-table-column label="文件名称" align="center" prop="filename" :show-overflow-tooltip="true"/>
      <el-table-column label="原始文件名" align="center" prop="originalFilename" :show-overflow-tooltip="true"/>
      <el-table-column label="文件扩展名" align="center" prop="ext" />
      <el-table-column label="存储平台" align="center" prop="platform" />
<!--      <el-table-column label="哈希信息" align="center" prop="hashInfo" />-->
<!--      <el-table-column label="图片预览" align="center">
        <template #default="scope">
          <template v-if="isImage(scope.row.ext)">
            <ImagePreview :src=scope.row.url width="20" height="20" />
          </template>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermission="['system:file:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <el-dialog v-model="openUploadDialog" title="上传文件" width="600px" append-to-body>
      <el-form :model="uploadForm" label-width="80px">
        <el-form-item label="上传类型">
          <el-radio-group v-model="uploadForm.uploadType">
            <el-radio value="image">图片</el-radio>
            <el-radio value="file">文件</el-radio>
            <el-radio value="chunk">分片上传</el-radio>
          </el-radio-group>
        </el-form-item>
        <div v-if="uploadForm.uploadType !== 'chunk'">
          <el-form-item v-if="uploadForm.uploadType === 'image'" label="图片上传">
            <ImageUpload :limit="5" :fileSize="10" isShowTip :uploadImgUrl="uploadUrl"
                         @update:modelValue="onUploadSuccess" style="width:100%" v-model="fileList"
                         :fileType="['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg']" />
          </el-form-item>
          <el-form-item v-else label="文件上传">
            <FileUpload :limit="5" :fileSize="10" isShowTip :uploadFileUrl="uploadUrl"
                        @update:modelValue="onUploadSuccess" style="width:100%" v-model="fileList"
                        :fileType="['doc', 'xls', 'ppt', 'txt', 'pdf', 'zip', 'rar', '7z', 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg', 'docx']" />
          </el-form-item>
        </div>
        <div v-else>
          <ChunkUpload :chunk-size="10" :maxConcurrency="4" @update:modelValue="onUploadSuccess" />
        </div>
      </el-form>
    </el-dialog>

  </div>
</template>

<script setup name="Detail">
import ImagePreview from "@/components/ImagePreview/index.vue";
import ImageUpload from '@/components/ImageUpload/index.vue';
import FileUpload from '@/components/FileUpload/index.vue';
import ChunkUpload from "@/components/ChunkUpload/index.vue"; // 引入分片上传组件
import { listDetail,delDetail} from "@/api/system/file";
import { ref, reactive, onMounted, toRefs, getCurrentInstance } from 'vue';

const { proxy } = getCurrentInstance();

const detailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true)
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    originalFilename: null,
  },
  rules: {
  }
});

const { queryParams } = toRefs(data);

const openUploadDialog = ref(false);

const uploadForm = reactive({
  uploadType: 'image', // 新增上传类型，默认图片
});

function onUploadSuccess() {
  openUploadDialog.value = false;

  getList();
}

function isImage(fileType) {
  if (!fileType) return false;
  return ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg'].includes(fileType.toLowerCase());
}

onMounted(() => {
  getList();
});

/** 查询文件记录列表 */
function getList() {
  loading.value = true;
  listDetail(queryParams.value).then(response => {
    detailList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    url: null,
    size: null,
    filename: null,
    originalFilename: null,
    path: null,
    ext: null,
    platform: null,
    hashInfo: null,
  };
  proxy.resetForm("detailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除当前数据项？').then(function() {
    return delDetail(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
